package com.mycompany.myapp.service.impl;

import com.mycompany.myapp.domain.UserClass;
import com.mycompany.myapp.repository.UserClassRepository;
import com.mycompany.myapp.service.UserClassService;
import com.mycompany.myapp.service.dto.UserClassDTO;
import com.mycompany.myapp.service.mapper.UserClassMapper;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service Implementation for managing {@link UserClass}.
 */
@Service
@Transactional
public class UserClassServiceImpl implements UserClassService {

    private final Logger log = LoggerFactory.getLogger(UserClassServiceImpl.class);

    private final UserClassRepository userClassRepository;

    private final UserClassMapper userClassMapper;

    public UserClassServiceImpl(UserClassRepository userClassRepository, UserClassMapper userClassMapper) {
        this.userClassRepository = userClassRepository;
        this.userClassMapper = userClassMapper;
    }

    @Override
    public UserClassDTO save(UserClassDTO userClassDTO) {
        log.debug("Request to save UserClass : {}", userClassDTO);
        UserClass userClass = userClassMapper.toEntity(userClassDTO);
        userClass = userClassRepository.save(userClass);
        return userClassMapper.toDto(userClass);
    }

    @Override
    public Optional<UserClassDTO> partialUpdate(UserClassDTO userClassDTO) {
        log.debug("Request to partially update UserClass : {}", userClassDTO);

        return userClassRepository
            .findById(userClassDTO.getId())
            .map(existingUserClass -> {
                userClassMapper.partialUpdate(existingUserClass, userClassDTO);

                return existingUserClass;
            })
            .map(userClassRepository::save)
            .map(userClassMapper::toDto);
    }

    @Override
    @Transactional(readOnly = true)
    public Page<UserClassDTO> findAll(Pageable pageable) {
        log.debug("Request to get all UserClasses");
        return userClassRepository.findAll(pageable).map(userClassMapper::toDto);
    }

    @Override
    @Transactional(readOnly = true)
    public Optional<UserClassDTO> findOne(Long id) {
        log.debug("Request to get UserClass : {}", id);
        return userClassRepository.findById(id).map(userClassMapper::toDto);
    }

    @Override
    public void delete(Long id) {
        log.debug("Request to delete UserClass : {}", id);
        userClassRepository.deleteById(id);
    }
}
